package org.parademo.equations.base;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Random;

public class TestEquation implements TestEquationInterface {
	
	public static final double e = 2.71828182845904523536;
	
	/**
	 * To set the number of voters.
	 */
	public int TOTAL_VOTERS = 100;
	
	/**
	 * Weighted votes for each voter, calculated and updated.
	 */
	ArrayList<Double> weightedAverages = new ArrayList<Double>(TOTAL_VOTERS);
	
	/**
	 * Individual randomly generated voting timings and tokens. 
	 */
	int[] timings = new int[TOTAL_VOTERS];
	int[] tokens = {1,-2,-1,2,1,-4,-2,-1,2,-3,3,-3,-3,-2,1,-2,5,-3,-3,2,-1,2,-5,-1,-1,5,-2,-2,5,-1,0,5,-1,-5,-5,-5,-2,-1,0,1,1,0,-2,-5,3,-3,-3,3,0,-2,2,4,1,-1,3,3,-3,2,0,-3,4,1,4,1,4,1,5,-5,2,1,0,-1,0,4,4,-1,1,-5,-2,4,0,1,-5,-1,-1,-3,-3,2,2,4,-3,-5,0,4,-1,-2,-1,5,0,2};
	
	/**
	 * Used for generating the tokens and list of timings.
	 */
	Random randomGenerator = new Random();

	/**
	 * Weighted totals for and against the proposal.
	 */
	double Wa = 0.0;
	double Wf = 0.0;
	
	/**
	 * Number of votes.
	 */
	int plusVotes = 43;
	int negativeVotes = 41;
	
	/**
	 * Total of tokens.
	 */
	int plusTotal = 125;
	int negativeTotal = -119;
	
	/**
	 * Temporary variables.
	 */
	int x = 0,y = 0;

	/**
	 * Implementation of the function.
	 */
	public double calculate(double index) {
		
		double result =  (-1 * Math.pow(index, 2.0) )/(1900 - Math.pow(index, 2.0));
		return Math.pow(e, result);
	}

	public void updateWeightedAverage() {
		
		double temp = 0.0;
		
		for (int i=0; i < TOTAL_VOTERS; i++) {
			
			//x = (randomGenerator.nextBoolean() ? 1:-1) * randomGenerator.nextInt(6);
			this.x = tokens[i];
			//System.out.println("The value of token is : " + x + "\n");
			this.y = randomGenerator.nextInt(30);
			//System.out.println("The value of timing is : " + y + "\n");
			
			//tokens[i] = x;
			this.timings[i] = y;
			
			if (x > 0) {
				
				//plusVotes++;
				//plusTotal += x;
				
				temp = x * calculate(y);
				this.weightedAverages.add(new Double(temp));
				
				Wf += temp;
				
			} else if (x < 0) {
				
				//negativeVotes++;
				//negativeTotal += x;
				
				temp = x * calculate(y);
				this.weightedAverages.add(new Double(temp));
				
				Wa += temp;
				
			} else if (x == 0)
				this.weightedAverages.add(0.0);
			
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		Writer output = null;
	    File file = new File("output.txt");
	    
	    try {
			output = new BufferedWriter(new FileWriter(file));
			String t1 = "";
			
			output.write ("Output of TestEquation\n");
            output.write ("----------------------\n\n");
			output.write ("Test cycle of 100 iterations with the votes and tokens as follows: \n\n");
			output.write ("The Tokens array is : -4,-1,-4,-5,4,0,5,5,0,3,-5,1,0,1,4,-4,1,-3,1,0,-5,1,0,0,-1,-5,-5,-2,1,-3,-5,3,0,0,0,-1,5,3,3,0,-2,1,1,0,-4,-5,-2,0,1,-1,3,5,-5,-4,-2,0,-5,1,-2,4,-3,5,-4,3,-1,-5,0,-1,-1,-3,-2,0,-1,-5,1,-1,-2,2,0,1,-2,2,3,-5,-4,4,-4,-4,-4,0,5,-5,-3,0,-2,-1,-3,1,0,-3 \n");
			output.write ("The total votes for : 42 \n");
			output.write ("The total votes against : 45 \n");
			
			output.write ("The total tokens for : 130 \n");
			output.write ("The total tokens against : -145 \n");
			
			for(int z=0; z<100; z++) {
				
				TestEquation test = new TestEquation();
				test.updateWeightedAverage();
				t1 = "";
		    
				for (int ind : test.timings) {
					
					t1 += (test.timings[ind] + ",");
				}
				
				output.write("************************************************************ \n");
				output.write("The timings array : "+ t1 + "\n");
				output.write ("The weighted total for : "+ test.Wf + "\n");
				output.write ("The weighted total against : "+ test.Wa + "\n");
				output.write("************************************************************  \n");
	    	}
			output.close();
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}

}
